#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int singleNonDuplicate(vector<int>& nums) {
        int left = 0, right = nums.size() - 1;
        while (left <= right)
        {
            int mid = (right - left) / 2 + left;
            if (mid == 0) return nums[mid];
            else if (mid % 2 == 0)
            {
                if (nums[mid] == nums[mid + 1]) left = mid;
                else if (nums[mid] == nums[mid - 1]) right = mid;
                else return nums[mid];
            }
            else
            {
                if (nums[mid] == nums[mid - 1]) left = mid + 1;
                else if (nums[mid] == nums[mid + 1]) right = mid - 1;
                else return nums[mid];
            }
        }
        return -1;
    }
};